package Bank_State_17;

public class Account {
    private double balance;//初始余额
    private AccountState state;//账户状态

    public Account(){
        this.balance = 1000;
        this.state = new NormalState();
        System.out.println("账户创建成功，初始余额为：" + balance);
    }

    public void setState(AccountState state){
        this.state = state;
    }

    public void StateCheck(){
        if( balance >= 0 ){
            this.state = new NormalState();
            System.out.println("当前账户状态正常");
        }
        if( balance > -2000 && balance < 0 ){
            this.state = new OverDraftState();
            System.out.println("当前账户状态为透支");
        }
        if( balance <= -2000 ){
            this.state = new RestrictedState();
            System.out.println("当前账户状态受限");
        }
    }

    public void deposit(double amount){
        /*
        if( !(state instanceof RestrictedState) ){
            state.deposit(amount);//不受限制状态下才允许存款
            this.balance += amount;
        }
         */
        state.deposit(amount);
        this.balance += amount;
        System.out.println("当前余额为：" + balance);
        this.StateCheck();
    }

    public void withdraw(double amount){
//        if( state instanceof NormalState ){
//            if( amount <= balance ){
//                state.withdraw(amount);
//                this.balance -= amount;
//            } else {
//                System.out.println("用户余额不足");
//            }
//        }
//        else if( state instanceof RestrictedState ){
//            System.out.println("用户账户受限，无法取款");
//        }
//        else if( state instanceof OverDraftState ){
//            System.out.println("用户账户透支，无法取款");
//        }
        state.withdraw(amount);
        this.balance -= amount;
        System.out.println("当前余额为：" + balance);
        this.StateCheck();
    }

    public void computerInterest(){
//        if( state instanceof NormalState ){
//            state.computerInterest();
//        }
//        else if( state instanceof RestrictedState ){
//            System.out.println("用户账户受限，无法计算利息");
//        }
//        else if( state instanceof OverDraftState ){
//            System.out.println("用户账户透支，无法计算利息");
//        }
        state.computerInterest();
        this.StateCheck();
    }
}
